package com.fy.controller;


import com.fy.dao.Ac_infoMapper;
import com.fy.dao.U_coMapper;
import com.fy.dao.U_enMapper;
import com.fy.dao.User_infoMapper;
import com.fy.entity.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/uen")
public class U_enController {
    String id = "1";


    @Resource
    U_enMapper u_enMapper;
    @Resource
    Ac_infoMapper acInfoMapper;
    @Resource
    User_infoMapper userInfoMapper;

    @GetMapping("/getAll")
    public List<U_en> getAll() {
        return u_enMapper.findAll();
    }

    @PostMapping("/getInfo")
    public List<U_en> getInfo(String info_id) {
        return u_enMapper.getInfo(info_id);
    }

    @PostMapping("/insert")
    public String insert(String en_id, String ac_id, String info_id, String name, String tel) {
        u_enMapper.insert(id, ac_id, info_id, name, tel);
        id = (Integer.valueOf(id) + 1 + "");
        acInfoMapper.update_renshu(1, ac_id);
        userInfoMapper.update_info(null, null, null, null, 2, info_id);

        return "200";
    }

    @PostMapping("/update")
    public String update(String info_id, String name, String tel, String ac_id) {
        List<U_en> all = u_enMapper.findAll();
        for (U_en u : all) {
            if (name == null) {
                if (u.getInfo_id().equals(info_id)) {
                    name = u.getName();
                }
            }
            if (tel == null) {
                if (u.getInfo_id().equals(info_id)) {
                    tel = u.getTel();
                }
            }

        }
        u_enMapper.update(name, tel, info_id, ac_id);
        return "200";
    }

    @PostMapping("/delete")
    public int delete(String info_id, String ac_id) {
        return u_enMapper.delete(info_id, ac_id);
    }

    @PostMapping("getAcInfoByInfoId")
    public List<? extends List<? extends Object>> getAcInfoById(String info_id) {
        List<List<Ac_info>> acInfos= new ArrayList<>();
        List<String> id = u_enMapper.getAcIdByInfoId(info_id);
        for (String s : id) {
            List<Ac_info> ac_infos = acInfoMapper.getInfo(s);
            if (!ac_infos.isEmpty()) {
                acInfos.add(ac_infos);
            }
        }
        return acInfos;

    }
}
